package com.acme.bankapp.domain.bank;

public class CheckingAccount extends AbstractAccount {
	private final String OVERDRAFT_MUST_BE_POSITIVE = "Overdraft must be positive";

	private double overdraft;

	public CheckingAccount(final double amount, final double overdraft) {
		super(amount);
		if (overdraft < 0) {
			throw new IllegalArgumentException(OVERDRAFT_MUST_BE_POSITIVE);
		}
		this.overdraft = overdraft;
	}

	@Override
	public void deposit(final double amount) {
		if (amount < 0) {
			throw new IllegalArgumentException(NEGATIVE_VALUE);
		}
		setBalance(balance + amount);

	}

	/**
	 * if balance is not enough for withdraw, use overdraft's resources
	 * */
	@Override
	public void withdraw(final double amount) {
		if (amount > maximumAmountTowothdraw()) {
			throw new IllegalArgumentException(NOT_ENOUGH_FOUNDS);
		}
		if (amount > balance) {
			setBalance(0);
			setOverdraft(overdraft - (amount - balance));
		} else {
			setBalance(balance - amount);
		}
	}
	
	@Override
	public double maximumAmountTowothdraw() {
		return getBalance() + getOverdraft();
	}

	public double getOverdraft() {
		return overdraft;
	}

	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}

}
